package com.war.demo.service.controller;

//import com.alibaba.druid.util.StringUtils;

import com.war.demo.service.enmu.ApiResponse;
import com.war.demo.service.enmu.ResultCode;
import com.war.demo.service.error.BusinessException;
import com.war.demo.service.service.LoginService;
import com.war.demo.service.util.ApiResponseUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户类
 *
 * @ApiParam(required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”,dateType="变量类型”,paramType="请求方式”）
 * @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”）
 */
@Api(value = "swagger2的demo例子", description = "用户登录接口", tags = "springboot-api", protocols = "HTTP", produces = "application/json", position = 2)
@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @GetMapping("/user/login")
    @ResponseBody
    @ApiOperation(value = "根据用户手机号和密码登录", notes = "登录请求接口", httpMethod = "GET", response = ApiResponse.class)
    @ApiResponses(value = {
            @io.swagger.annotations.ApiResponse(code = 200, message = "{\"code\":200,\"message\":\"success\",\"data\":{\"userId\":\"15103747270\",\"user_name\":\"火柴\",\"telphone\":\"15103747270\",\"token\":\"eyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTY}}"),
            @io.swagger.annotations.ApiResponse(code = 203, message = "{\"code\":203,\"message\":\"用户名为空\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 208, message = "{\"code\":208,\"message\":\"密码不能为空\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 201, message = "{\"code\":201,\"message\":\"密码错误\",\"data\":null}"),
            @io.swagger.annotations.ApiResponse(code = 202, message = "{\"code\":202,\"message\":\"用户名错误\",\"data\":null}")}
    )
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "telphone", value = "用户手机号", required = true, dataType = "String", paramType = "query")
                    , @ApiImplicitParam(name = "password", value = "用户密码", required = true, dataType = "String", paramType = "query")})
    public ApiResponse Login(String telphone, String password) throws BusinessException {
//        if (StringUtils.isEmpty(telphone)) {
//            throw new BusinessException(ResultCode.USERNAME_NULL);
//        }
//        if (StringUtils.isEmpty(password)) {
//            throw new BusinessException(ResultCode.VERIFY_PASSWORD_NULL);
//        }
        ApiResponse lc = loginService.verifyLogin(telphone, password);
        System.out.println("登录反馈： " + lc.toString());
        return ApiResponseUtil.getResult(lc);
    }

}
